<?php
class App_Mail {
    /**
     * the mail transport method
     * @var Zend_Mail_Transport
     */
    private $_transport = null;
    /**
     * the zend mail object
     * @var Zend_Mail
     */
    private $_mail;
    
    /**
     * The default email sender
     * @var string
     */
    private $_from;
    
    private $_enabled = true;

    /**
     * set up the mail object
     *
     */
    public function __construct() {
        $appConfig = Zend_Registry::get("appConfig");
        if($appConfig->get('use_smtp_mail') == 1) {
            $config = array(
                'auth'=>'Login',
                'username'=>$appConfig->get('smtp_username'),
                'password'=>$appConfig->get('smtp_password')
            );
            $this->_transport = new Zend_Mail_Transport_Smtp($appConfig->get('smtp_host'), $config);
        }
        $this->_from = $appConfig->get('smtp_from_default');
        $this->_enabled = ($appConfig->get('enable_mail') == 1)?true:false;
        $this->_mail = new Zend_Mail();
    }

    /**
     * send the message in text mode
     *
     * @param string $recipient
     * @param string $from
     * @param string $subject
     * @param string $template
     * @param array $data
     * @param string $cc
     * @return bool
     */
    public function sendText($recipient, $subject, $message, $cc = false) {
        if(!$this->_enabled) {
            return;
        }
        $this->_mail->setBodyText($message);
        $this->_mail->setFrom($this->_from);
        if(is_array($recipient)) {
            foreach($recipient as $value) {
                $this->_mail->addTo($value);
            }
        } else {
            $this->_mail->addTo($recipient);
        }
        if($cc) {
            $this->_mail->addCc($cc);
        }
        $this->_mail->setSubject($subject);
        return $this->_mail->send($this->_transport);
    }
    
    /**
     * send the message in html mode
     *
     * @param string $recipient
     * @param string $from
     * @param string $subject
     * @param string $template
     * @param array $data
     * @param string $cc
     * @return bool
     */
    public function sendHtml($recipient, $subject, $message, $cc = false) {
        if(!$this->_enabled) {
            return;
        }
        $this->_mail->setBodyHtml($message);
        $this->_mail->setFrom($this->_from);
        if(is_array($recipient)) {
            foreach($recipient as $value) {
                $this->_mail->addTo($value);
            }
        } else {
            $this->_mail->addTo($recipient);
        }
        if($cc) {
            $this->_mail->addCc($cc);
        }
        $this->_mail->setSubject($subject);
        return $this->_mail->send($this->_transport);
    }
    
	/**
     * @return the $_from
     */
    public function getFrom() {
        return $this->_from;
    }

	/**
     * @param $_from the $_from to set
     */
    public function setFrom($_from) {
        $this->_from = $_from;
    }
	/**
     * @return Zend_Mail
     */
    public function getMail() {
        return $this->_mail;
    }


}//fin de la clase